package com.ogawa.jopai.devicemanage.activity

import android.graphics.Color
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.ogawa.base.base.BaseVMActivity
import com.ogawa.base.bean.LoadState
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.ogawa.projectcommon.constants.UrlPathConstant.ACTIVITY_DEVICEMANAGE_CHOOSE_DEVICE
import com.ogawa.projectcommon.utils.DataStatusViewUtils
import com.ogawa.base.utils.DividerGridItemDecoration
import com.ogawa.base.utils.toPx
import com.ogawa.jopai.devicemanage.adapter.DeviceSelectAdapter
import com.ogawa.jopai.devicemanage.viewmodel.DeviceSelectViewModel
import com.ogawa.joypal.devicemanage.R
import com.ogawa.projectcommon.bean.ProjectConfigWifiDeviceBean
import com.ogawa.projectcommon.utils.ProjectSpUtils
import kotlinx.android.synthetic.main.activity_select_device.*

@Route(path = ACTIVITY_DEVICEMANAGE_CHOOSE_DEVICE)
class SelectDeviceActivity : BaseVMActivity<DeviceSelectViewModel>() {
    private lateinit var mSelectAdapter :DeviceSelectAdapter

    override fun getLayoutId(): Int {
        return R.layout.activity_select_device
    }


    override fun initView() {
        super.initView()
        setTitle(getString(R.string.choose_device))
        initRv()
        swipeRefresh.setOnRefreshListener {
            swipeRefresh.isRefreshing = false
            mViewModel.getDeviceTypes()
        }
        mViewModel.getDeviceTypes()

    }

    private fun initRv(){
        selectDeviceRv.layoutManager = GridLayoutManager(this,2)
        selectDeviceRv.setHasFixedSize(true)
//        var emptyView = DataStatusViewUtils.getView(this,
//            DataStatusViewUtils.DATA_EMPTY,"暂无设备", Color.TRANSPARENT,null)
//        emptyView.setPadding(0,0,0,0)
        selectDeviceRv.addItemDecoration(DividerGridItemDecoration(2,20.toPx,20.toPx,true))
        mSelectAdapter = DeviceSelectAdapter()
        mSelectAdapter.setOnItemClickListener { _, _, position ->
            val deviceTypeBean = mSelectAdapter.data[position]
            ProjectSpUtils.setProjectConfigWifiDeviceBean(ProjectConfigWifiDeviceBean().also {
                it.wifiName = ""
                it.wifiPwd = ""
                it.deviceName = deviceTypeBean.typeName
                it.devicePwd = ""
                it.deviceSN = ""
                it.selectRoom = ""
                it.id = deviceTypeBean.id
                it.imageSrc = deviceTypeBean.imageSrc
                it.typeName = deviceTypeBean.typeName
                it.bluetoothRules = deviceTypeBean.bluetoothRules
                it.typeCode = deviceTypeBean.typeCode
                it.netMethod = deviceTypeBean.netMethod
                it.wifiRules = deviceTypeBean.wifiRules

            })
            ARouter.getInstance().build(UrlPathConstant.ACTIVITY_DISTRIBUTIONNETWORK_START_CONNECT).navigation()
            finish()
        }
//        mSelectAdapter.emptyView = emptyView
        selectDeviceRv.adapter = mSelectAdapter
    }
    fun emptyView(){
        var emptyView = DataStatusViewUtils.getView(this,
            DataStatusViewUtils.DATA_EMPTY,"暂无设备", Color.TRANSPARENT,null)
        emptyView.setPadding(0,0,0,0)
        if (mSelectAdapter.data.isEmpty()){
            mSelectAdapter.emptyView = emptyView
        }
    }
    override fun providerVMClass(): Class<DeviceSelectViewModel>? {
        return DeviceSelectViewModel::class.java
    }

    override fun observeVM() {
        mViewModel.loadState.observe(this, Observer {
            when (it) {
                is LoadState.Loading -> {
                    showLoadingDialog(canceledOnTouchOutside = false)
                }
                is LoadState.Fail -> {
                    dismissLoadingDialog()
                    ToastUtils.showShort(it.msg)
                    LogUtils.e(it.msg)
                    emptyView()
                }
                is LoadState.Success -> {
                    dismissLoadingDialog()
                }
            }
        })
        mViewModel.deviceTypeBeans.observe(this, Observer {
            mSelectAdapter.setNewData(it)
            emptyView()
        })
    }

}